package com.tanhua.dubbo.api;


import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {
        //根据toUserId查询，根据评分score排序，获取第一条
        //构建Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //构建Query对象,根据Score获得最高分用户
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score"))).limit(1);
        //调用mongoTemplate查询

        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    @Override
    public PageResult queryRecommendUserList(Integer page, Integer pageSize, Long toUserId) {
        //根据touserid构建Criteria对象
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //构建分页构造器,按照score降序排序
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score")))
                .limit(pageSize).skip((page - 1)*pageSize );
        //3、调用mongoTemplate查询
        List<RecommendUser> lists = mongoTemplate.find(query, RecommendUser.class);
        long count = mongoTemplate.count(query, RecommendUser.class);
        return new PageResult(page,pageSize , count, lists);
    }
    /*根据用户ID查询推荐者的缘分值*/
    @Override
    public RecommendUser queryByUserId(Long userId, Long toUserId) {
        //1.根据两者id构建查询条件
        Criteria criteria=Criteria.where("toUserId").is(toUserId).and("userId").is(userId);
        Query query= Query.query(criteria);
        RecommendUser user = mongoTemplate.findOne(query, RecommendUser.class);
        if (user==null){
            //创建对象,设定缘分值
            user = new RecommendUser();
            user.setUserId(userId);
            user.setToUserId(toUserId);
            //构建缘分值
            user.setScore(95d);
        }
         return  user;
    }
    /**
     * 查询探花列表，查询时需要排除喜欢和不喜欢的用户
     * 1、排除喜欢，不喜欢的用户
     * 2、随机展示
     * 3、指定数量
     */
    @Override
    public List<RecommendUser> queryCardsList(Long userId, int count) {
      //1,排除喜欢的不喜欢的用户?如何排除,先查找出来,
        Query query=Query.query(Criteria
                .where("userId").is(userId));
        List<UserLike> likeList = mongoTemplate.find(query, UserLike.class);
        List<Long> likeUserIdS = CollUtil.getFieldValues(likeList, "likeUserId", Long.class);
        //2、构造查询推荐用户的条件
        Criteria criteria = Criteria.where("toUserId").is(userId).and("userId").nin(likeUserIdS);
        //3、使用统计函数，随机获取推荐的用户列表
        TypedAggregation<RecommendUser> newAggregation = TypedAggregation.newAggregation(RecommendUser.class,
                Aggregation.match(criteria),//指定查询条件
                Aggregation.sample(count)
        );
        AggregationResults<RecommendUser> results = mongoTemplate.aggregate(newAggregation, RecommendUser.class);
        //4、构造返回
        return results.getMappedResults();
    }
}